/*==================== 6-1 按等级统计学生成绩 ====================*/
int set_grade(struct student *p, int n)
{
    int failCnt = 0;
    for (int i = 0; i < n; ++i) {
        int s = p[i].score;
        if (s >= 85)
            p[i].grade = 'A';
        else if (s >= 70)
            p[i].grade = 'B';
        else if (s >= 60)
            p[i].grade = 'C';
        else {
            p[i].grade = 'D';
            ++failCnt;
        }
    }
    return failCnt;
}
/*==================== 6-2 修改学生成绩 ====================*/
int update_score(struct student *p, int n, int num, int course, int score)
{
    for (int i = 0; i < n; ++i) {
        if (p[i].num == num) {           
            switch (course) {         
                case 1: p[i].math     = score; break;
                case 2: p[i].english  = score; break;
                case 3: p[i].computer = score; break;
                default:  break;
            }
            return i;                 
        }
    }
    return -1;                        
}
/*==================== 6-3 自动售货机 ====================*/
void menu(struct product p[], int n)
{
    for (int i = 0; i < n; ++i)
        printf("[%d] %s %.1f\n", p[i].id, p[i].name, p[i].price);
}

void order(struct product p[], int n)
{
    int id, qty;
    double total = 0.0;

    while (scanf("%d", &id) == 1 && id != 0) {
        scanf("%d", &qty);
        int idx = -1;
        for (int i = 0; i < n; ++i)
            if (p[i].id == id) { idx = i; break; }
        if (idx == -1) continue;                   

        double amount = p[idx].price * qty;
        total += amount;
        printf("%s * %d = %.1f\n", p[idx].name, qty, amount);
    }
    printf("Total = %.1f\n", total);
}
/*==================== 6-4 评委打分-n（完整 4 个函数） ====================*/
#include <string.h>      

struct Score {
    int  id;            
    char name[11];     
    int  value[17];    
    double finalScore;   
    int  rank;       
};

void GetScore(struct Score grade[], int n)
{
    for (int i = 0; i < n; ++i) {
        scanf("%d %s", &grade[i].id, grade[i].name);
        for (int j = 0; j < 17; ++j)
            scanf("%d", &grade[i].value[j]);
        grade[i].finalScore = 0.0;
        grade[i].rank       = 0;
    }
}

void CalcuScore(struct Score grade[], int n)
{
    for (int i = 0; i < n; ++i) {
        int sum = 0, min = 101, max = -1;
        for (int j = 0; j < 17; ++j) {
            int v = grade[i].value[j];
            sum += v;
            if (v < min) min = v;
            if (v > max) max = v;
        }
        grade[i].finalScore = (sum - min - max) / 15.0;   
    }
}

void Ranking(struct Score grade[], int n)
{
    for (int i = 0; i < n - 1; ++i) {
        int maxIdx = i;
        for (int j = i + 1; j < n; ++j)
            if (grade[j].finalScore > grade[maxIdx].finalScore)
                maxIdx = j;
        if (maxIdx != i) {
            struct Score tmp = grade[i];
            grade[i] = grade[maxIdx];
            grade[maxIdx] = tmp;
        }
        grade[i].rank = i + 1;
    }
    grade[n - 1].rank = n;        
}

void PrintScore(struct Score grade[], int n)
{
    for (int i = 0; i < n; ++i)
        printf("第%d名 %d %s %6.2f\n",
               grade[i].rank, grade[i].id,
               grade[i].name, grade[i].finalScore);
}

/*==================== 6-5 评委打分-t-CalcuScore（仅 1 个函数 + 结构体补全） ====================*/
struct Score {
    int  id;
    char name[11];
    int  value[17];
    double finalScore;
    int  rank;
};

void CalcuScore(struct Score grade[], int n)
{
    for (int i = 0; i < n; ++i) {
        int sum = 0, min = 101, max = -1;
        for (int j = 0; j < 17; ++j) {
            int v = grade[i].value[j];
            sum += v;
            if (v < min) min = v;
            if (v > max) max = v;
        }
        grade[i].finalScore = (sum - min - max) / 15.0;
    }
}

/* ================== 6-6 评委打分-t-Ranking ================== */
#include <math.h>   

struct Score {
    int    id;       
    char   name[11];    
    int    value[17]; 
    double finalScore; 
    int    rank;      
};

void Ranking(struct Score grade[], int n)
{
    for (int i = 0; i < n - 1; ++i) {
        int maxIdx = i;
        for (int j = i + 1; j < n; ++j) {
            if (grade[j].finalScore > grade[maxIdx].finalScore
                || (fabs(grade[j].finalScore - grade[maxIdx].finalScore) < 1e-6
                    && grade[j].id < grade[maxIdx].id))   
            {
                maxIdx = j;
            }
        }
        if (maxIdx != i) {
            struct Score tmp = grade[i];
            grade[i] = grade[maxIdx];
            grade[maxIdx] = tmp;
        }
        grade[i].rank = i + 1;          
    }
    grade[n - 1].rank = n;           
}
